﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Pages_Checkout : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Title = PresentsConfiguration.SiteName + " : Checkout";
        if (!IsPostBack)
            PopulateControls();
    }

    // fill controls with data
    private void PopulateControls()
    {
        // get the items in the shopping cart
        DataTable dt = ShoppingCartAccess.GetItems();
        // populate the list with the shopping cart contents
        grid.DataSource = dt;
        grid.DataBind();
        // setup controls
        titleLabel.Text =
        "Здесь представлены товары, находящиеся в вашей корзине:";
        grid.Visible = true;
        // display the total amount
        decimal amount = ShoppingCartAccess.GetTotalAmount();
        totalAmountLabel.Text = String.Format("{0:c}", amount);
        // check customer details
        bool addressOK = true;
        bool cardOK = true;
        if (Profile.Address1 + Profile.Address2 == "")
        {
            addressOK = false;
        }
        if (Profile.CreditCard == "")
        {
            cardOK = false;
        }
        // report / hide place order button
        if (!addressOK)
        {
            if (!cardOK)
            {
                InfoLabel.Text =
                "You must provide a valid address and credit card "
                + "before placing your order.";
            }
            else
            {
                InfoLabel.Text =
                "You must provide a valid address before placing your "
                + "order.";
            }
        }
        else if (!cardOK)
        {
            InfoLabel.Text = "You must provide a credit card before "
            + "placing your order.";
        }
        else
        {
            InfoLabel.Text = "Please confirm that the above details are "
            + "correct before proceeding.";
        }
        placeOrderButton.Visible = addressOK && cardOK;
    }
    protected void placeOrderButton_Click(object sender, EventArgs e)
    {
        // Store the total amount because the cart
        // is emptied when creating the order
        decimal amount = ShoppingCartAccess.GetTotalAmount();
        // Create the order and store the order ID
        string orderId = ShoppingCartAccess.CreateCommerceLibOrder();
        Response.Redirect("OrderPlaced.aspx");
        // Create the PayPal redirect location
        string redirect = "";
        redirect +=
        "https://www.paypal.com/xclick/business=youremail@server.com";
        redirect += "&item_name=BalloonShopOrder " + orderId;
        redirect += "&item_number=" + orderId;
        redirect += "&amount=" + String.Format("{0:c} ", amount);
        redirect += "&return=http://www.YourWebSite.com";
        redirect += "&cancel_return=http://www.YourWebSite.com";
        // Redirect to the payment page
        Response.Redirect(redirect);
    }
}